// pages/homeScreen/homeScreen.js
// 筛选页
Page({
  /**
   * 页面的初始数据
   */
  data: {
    homeScreenAreaIcon: '/assets/icons/more.png',
    cityName: '',
    cityId: null,
    currentSexId: null,
    currentAgeId: null,
    currentCategoryId: [],
    currentPriceId: null,
    minAge: null,
    maxAge: null,
    minPrice: null,
    maxPrice: null,
    sexList: [{
        id: 0,
        sex: '女',
        isSelected: false
      },
      {
        id: 1,
        sex: '男',
        isSelected: false
      }
    ],
    ageList: [{
        id: 1,
        age: '17岁以下',
        min: 0,
        max: 17,
        isSelected: false
      },
      {
        id: 2,
        age: '18-25岁',
        min: 18,
        max: 25,
        isSelected: false
      },
      {
        id: 3,
        age: '26-30岁',
        min: 26,
        max: 30,
        isSelected: false
      }
    ],
    categoryList: [],
    priceList: [{
        id: 1,
        price: '100-1000',
        min: 100,
        max: 1000,
        isSelected: false
      },
      {
        id: 2,
        price: '1000-10000',
        min: 1000,
        max: 10000,
        isSelected: false
      },
      {
        id: 3,
        price: '10000-20000',
        min: 10000,
        max: 20000,
        isSelected: false
      }
    ],
  },
  clickSexOptions: function(e) {
    // 性别选中项 
    let id = e.currentTarget.dataset.id
    this.setData({
      currentSexId: id,
    })
  },
  clickAgeOptions: function(e) {
    // 适用年龄选中项
    let id = e.currentTarget.dataset.id
    let min = e.currentTarget.dataset.min
    let max = e.currentTarget.dataset.max
    this.setData({
      currentAgeId: id,
      minAge: min,
      maxAge: max,
    })
  },
  clickCategory: function(e) {
    // 类目选中项
    let index = e.currentTarget.dataset.index
    let item = this.data.categoryList[index]
    item.isSelected = !item.isSelected
    let id = e.currentTarget.dataset.id
    let categoryIdArr = this.data.currentCategoryId
    this.setData({
      // 多选控制亮灯
      categoryList: this.data.categoryList
    })
    if (categoryIdArr.indexOf(id) < 0) {
      // 保存选中项id
      categoryIdArr.push(id)
      this.setData({
        currentCategoryId: categoryIdArr
      })
    } else if (categoryIdArr.indexOf(id) >= 0) {
      categoryIdArr.splice(categoryIdArr.indexOf(id), 1)
      this.setData({
        currentCategoryId: categoryIdArr
      })
    }
  },
  clickPrice: function(e) {
    // 案例价格区间选中项
    let id = e.currentTarget.dataset.id
    let min = e.currentTarget.dataset.min
    let max = e.currentTarget.dataset.max
    this.setData({
      currentPriceId: id,
      minPrice: min,
      maxPrice: max
    })
  },
  clickResetBtn: function() {
    // 重置
    let that = this
    this.setData({
      cityName: '',
      currentSexId: null,
      currentAgeId: null,
      minAge: null,
      maxAge: null,
      currentCategoryId: [],
      currentPriceId: null,
      minPrice: null,
      maxPrice: null
    })

    this.data.categoryList.forEach((item, index) => {
      that.setData({
        [`categoryList[${index}].isSelected`]: false
      })
    })
  },
  clickSureBtn: function() {
    let _this = this
    // 确定-发送选中项到服务器,返回搜索结果页，重新加载新数据
    wx.setStorage({
      key: 'sex',
      data: _this.data.currentSexId,
    })
    wx.setStorage({
      key: 'beginAge',
      data: _this.data.minAge,
    })
    wx.setStorage({
      key: 'endAge',
      data: _this.data.maxAge,
    })
    wx.setStorage({
      key: 'categoryIds',
      data: _this.data.currentCategoryId,
    })
    wx.setStorage({
      key: 'lowPrice',
      data: _this.data.minPrice,
    })
    wx.setStorage({
      key: 'highPrice',
      data: _this.data.maxPrice,
    })
    wx.setStorage({
      key: 'cityId',
      data: _this.data.cityId,
    })

    wx.redirectTo({
      url: '/pages/homeSearch/homeSearch?searchResultOpen=' + true,
    })
  },
  clickRegion: function() {
    // 跳转到地区页
    wx.redirectTo({
      url: '/pages/homeRegion/homeRegion',
    })
  },
  changeInput: function(e) {
    // 输入框-双向绑定
    let dataset = e.currentTarget.dataset
    let value = e.detail.value
    this.data[dataset.obj] = value
    this.setData({
      obj: this.data[dataset.obj]
    })
    if (dataset.crt == 'currentAgeId') {
      this.setData({
        currentAgeId: null
      })
    } else if (dataset.crt == 'currentPriceId') {
      this.setData({
        currentPriceId: null
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this
    let config = wx.getStorageSync('config')
    // 获取大类id判断筛选类目
    wx.getStorage({
      key: 'navid',
      success: function(res) {
        // 加载类目
        wx.request({
          url: config.host + 'api/product/findCategory',
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            type: Number(res.data)
          },
          success(res) {
            if (res.statusCode == 200 && res.data.status == 200) {
              _this.setData({
                categoryList: res.data.data
              })
            }
          }
        })
      },
    })

    // 获取城市地区
    if (options.cityName) {
      let cityName = options.cityName
      let cityId = options.cityId
      this.setData({
        cityName: cityName,
        cityId: cityId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  }
})